package com.example.test.mytestapplication.Utils;

/**
 * @author cuige E64667
 * @date 2018/9/11 19:17
 * @company Fotile智能厨电研究院
 * @description XXXX
 */

public class SortUtils {

    /*
    * 冒泡排序
    */
    public char[] bubbleSortSelect(char[] str) {
        for (int i = 0; i < str.length; i++) {
            for (int j = 0; j < str.length - 1 - i; j++) {
                if (str[j] > str[j + 1]) {
                    char temp = str[j];
                    str[j] = str[j + 1];
                    str[j + 1] = temp;
                }
            }
        }
        return str;
    }

    public int[] bubbleSortSelect(int[] arr) {
        int temp;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length - i - 1; j++) {
                if (arr[i] > arr[j]) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }

    /*
    * 插入排序
    * */
    public char[] InsertSort(char[] str) {

        int i, j;
        char insertData;

        for (i = 1; i < str.length; i++)  //从数组的第二个元素开始循环将数组中的元素插入
        {
            insertData = str[i];
            j = i - 1;
            while (j >= 0 && insertData < str[j]) {
                str[j + 1] = str[j];  //如果要插入元素小于第j个元素,就将第j个元素向后移动
                j--;
            }
            str[j + 1] = insertData;
        }
        return str;
    }


    public int[] InsertSort(int[] arr) {
        int i, j;
        int insertData;

        for (i = 1; i < arr.length; i++) {
            insertData = arr[i];
            j = i - 1;
            while (j >= 0 && insertData < arr[j]) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = insertData;
        }
        return arr;
    }

}
